<?php 
	
require_once("ui.php");

class SubcategoryItem {
	public $itemId;
	public $description;
	public $tabId;
	
	function __construct($id, $description, $tabId) {
		$this->itemId = $id;
		$this->description = $description;
		$this->tabId = $tabId;
	}
	
	public function getRadioButtonValue() {
		return $this->itemId;
	}
	
	public function getRadioGroupName() {
		// hack: duplicity with tab.getRadioGroupName
		return "radioGroup" . $this->tabId;
	}
	
	public function getRadioButtonId() {
		return "radioButton-" . $this->tabId . "-" . $this->itemId;  
	}
	
	public function printRadioButton($checked) {
		$name = $this->getRadioGroupName();
		$buttonId = $this->getRadioButtonId();
		$value = $this->getRadioButtonValue();
		
		echo '<label for="' . $buttonId . '">';

		printRadio($buttonId, "", $name, $value, $checked);
		
		echo $this->description;
		echo '</label><br>';
	}
}

class ReportSubcategory {
	public $name;
	public $subcategoryId;
	public $items;
	public $categoryId;
	public $tabId;

	function __construct($id, $name, $items, $categoryId, $tabId) {
		$this->subcategoryId = $id;
		$this->name = $name;
		$this->items = $items;
		$this->categoryId = $categoryId;
		$this->tabId = $tabId; 
	}

	public function getDivId() {
		return 'div' . $this->tabId . '-' . $this->categoryId . '-' . $this->subcategoryId; 
	}
		
	public function printSubcategoryDiv() {
		$items = $this->items; 
		
		echo '<div id="' . $this->getDivId() . '">';
		
		foreach ($items as $j => $item) {
			$groupName = $item->getRadioGroupName();
			$previousValue = getOriginalPost($groupName);			
			
			$itemValue = $item->getRadioButtonValue();
			$checked = ($itemValue == $previousValue);
			
			$item->printRadioButton($checked);
		}
		echo '</div>';		
	}
	
	public function generateJs($jsParentObject) {
		$categoryObject = $jsParentObject . "._" . $this->subcategoryId;
		 
		echo $categoryObject . ' = new Hash();' . PHP_EOL;
		echo $categoryObject . '.subcategoryId = "' .
			$this->subcategoryId . '";' . PHP_EOL;
	}
}

class ReportCategory {
	public $categoryId;
	public $categoryName;
	public $tabId;
	public $subcategories;
	
	function __construct($id, $name, $subcategories, $tabId) {
		$this->categoryId = $id;
		$this->categoryName = $name;
		$this->subcategories = $subcategories;
		$this->tabId = $tabId;
	}

	public function printCategoryContent() {
		
		// print select; one subcategory select for each category
		$this->printCategorySelect();
		
		// and then divs
		foreach ($this->subcategories as $i => $subcategory) {
			$subcategory->printSubcategoryDiv();
		}
	}
	
	public function getSelectId() {
		return 'select' . $this->tabId . '-' . $this->categoryId;
	}
	
	public function printCategorySelect() {
		$optionsInfo = array();

		foreach ($this->subcategories as $j => $subcategory) {
			$optionInfo['value'] = $subcategory->subcategoryId;
			$optionInfo['description'] = $subcategory->name;
			
			array_push($optionsInfo, $optionInfo);
		}
		
		printSelect($this->getSelectId(), 
			'selectSubcategory',
			'{prefix: "div", tabId : "' . $this->tabId . '", categoryId : "' . $this->categoryId . 
				'"}',
			$optionsInfo,
			null,
			"reportCategorySelect"
		);
	}
	
	public function getFirstSubcategoryKey() {
		reset($this->subcategories);
		return key($this->subcategories);		
	}

	public function generateJs($jsParentObject) {
		$firstSubcategoryKey = $this->getFirstSubcategoryKey();
		$firstSubcategory = $this->subcategories[$firstSubcategoryKey];
		 
		echo $categoryObject . ' = new Hash();' . PHP_EOL;
		echo $categoryObject . '.categoryId = "' .
			$this->categoryId . '";' . PHP_EOL;
		
		echo $categoryObject . '.firstSubcategory = "' .
			$firstSubcategory->subcategoryId . '";' . PHP_EOL;
			
		foreach ($this->subcategories as $i => $subcategory) {
			$subcategory->generateJs($categoryObject);
		}

	}
}
	
?>